<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登录账号" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入登录账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="md5密码盐" prop="salt">
        <el-input
          v-model="queryParams.salt"
          placeholder="请输入md5密码盐"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-input
          v-model="queryParams.avatar"
          placeholder="请输入头像"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker clearable
          v-model="queryParams.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="电子邮件" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入电子邮件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构编码" prop="orgCode">
        <el-input
          v-model="queryParams.orgCode"
          placeholder="请输入机构编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第三方登录的唯一标识" prop="thirdId">
        <el-input
          v-model="queryParams.thirdId"
          placeholder="请输入第三方登录的唯一标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="同步工作流引擎(1-同步,0-不同步)" prop="activitiSync">
        <el-input
          v-model="queryParams.activitiSync"
          placeholder="请输入同步工作流引擎(1-同步,0-不同步)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号，唯一键" prop="workNo">
        <el-input
          v-model="queryParams.workNo"
          placeholder="请输入工号，唯一键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职务，关联职务表" prop="post">
        <el-input
          v-model="queryParams.post"
          placeholder="请输入职务，关联职务表"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="座机号" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入座机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份" prop="userIdentity">
        <el-input
          v-model="queryParams.userIdentity"
          placeholder="请输入身份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="多租户标识" prop="relTenantIds">
        <el-input
          v-model="queryParams.relTenantIds"
          placeholder="请输入多租户标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ID" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workSafetyStandardization:sysUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['workSafetyStandardization:sysUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workSafetyStandardization:sysUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['workSafetyStandardization:sysUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sysUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="登录账号" align="center" prop="username" />
      <el-table-column label="真实姓名" align="center" prop="realname" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="md5密码盐" align="center" prop="salt" />
      <el-table-column label="头像" align="center" prop="avatar" />
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别(0-默认未知,1-男,2-女)" align="center" prop="sex" />
      <el-table-column label="电子邮件" align="center" prop="email" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="机构编码" align="center" prop="orgCode" />
      <el-table-column label="性别(1-正常,2-冻结)" align="center" prop="status" />
      <el-table-column label="第三方登录的唯一标识" align="center" prop="thirdId" />
      <el-table-column label="第三方类型" align="center" prop="thirdType" />
      <el-table-column label="同步工作流引擎(1-同步,0-不同步)" align="center" prop="activitiSync" />
      <el-table-column label="工号，唯一键" align="center" prop="workNo" />
      <el-table-column label="职务，关联职务表" align="center" prop="post" />
      <el-table-column label="座机号" align="center" prop="telephone" />
      <el-table-column label="身份" align="center" prop="userIdentity" />
      <el-table-column label="负责部门" align="center" prop="departIds" />
      <el-table-column label="多租户标识" align="center" prop="relTenantIds" />
      <el-table-column label="设备ID" align="center" prop="clientId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workSafetyStandardization:sysUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workSafetyStandardization:sysUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="登录账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入登录账号" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="md5密码盐" prop="salt">
          <el-input v-model="form.salt" placeholder="请输入md5密码盐" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="电子邮件" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮件" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="机构编码" prop="orgCode">
          <el-input v-model="form.orgCode" placeholder="请输入机构编码" />
        </el-form-item>
        <el-form-item label="删除状态(0-正常,1-已删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除状态(0-正常,1-已删除)" />
        </el-form-item>
        <el-form-item label="第三方登录的唯一标识" prop="thirdId">
          <el-input v-model="form.thirdId" placeholder="请输入第三方登录的唯一标识" />
        </el-form-item>
        <el-form-item label="同步工作流引擎(1-同步,0-不同步)" prop="activitiSync">
          <el-input v-model="form.activitiSync" placeholder="请输入同步工作流引擎(1-同步,0-不同步)" />
        </el-form-item>
        <el-form-item label="工号，唯一键" prop="workNo">
          <el-input v-model="form.workNo" placeholder="请输入工号，唯一键" />
        </el-form-item>
        <el-form-item label="职务，关联职务表" prop="post">
          <el-input v-model="form.post" placeholder="请输入职务，关联职务表" />
        </el-form-item>
        <el-form-item label="座机号" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入座机号" />
        </el-form-item>
        <el-form-item label="身份" prop="userIdentity">
          <el-input v-model="form.userIdentity" placeholder="请输入身份" />
        </el-form-item>
        <el-form-item label="负责部门" prop="departIds">
          <el-input v-model="form.departIds" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="多租户标识" prop="relTenantIds">
          <el-input v-model="form.relTenantIds" placeholder="请输入多租户标识" />
        </el-form-item>
        <el-form-item label="设备ID" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入设备ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysUser, getSysUser, delSysUser, addSysUser, updateSysUser } from "@/api/workSafetyStandardization/sysUser";

export default {
  name: "SysUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      sysUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        realname: null,
        password: null,
        salt: null,
        avatar: null,
        birthday: null,
        sex: null,
        email: null,
        phone: null,
        orgCode: null,
        status: null,
        thirdId: null,
        thirdType: null,
        activitiSync: null,
        workNo: null,
        post: null,
        telephone: null,
        userIdentity: null,
        departIds: null,
        relTenantIds: null,
        clientId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listSysUser(this.queryParams).then(response => {
        this.sysUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        realname: null,
        password: null,
        salt: null,
        avatar: null,
        birthday: null,
        sex: null,
        email: null,
        phone: null,
        orgCode: null,
        status: null,
        delFlag: null,
        thirdId: null,
        thirdType: null,
        activitiSync: null,
        workNo: null,
        post: null,
        telephone: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userIdentity: null,
        departIds: null,
        relTenantIds: null,
        clientId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSysUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSysUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + ids + '"的数据项？').then(function() {
        return delSysUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('workSafetyStandardization/sysUser/export', {
        ...this.queryParams
      }, `sysUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
